package chenbi.renthouse.controller;

import chenbi.renthouse.common.Result;
import chenbi.renthouse.controller.dto.LoginDTO;
import chenbi.renthouse.controller.request.LoginRequest;
import chenbi.renthouse.controller.request.UserPageRequest;
import chenbi.renthouse.entity.User;
import chenbi.renthouse.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

//    登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request) {
        LoginDTO login = userService.login(request);

        return Result.success(login);
    }


//    新增用户
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

//    通过ID查询，获取用户数据
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

//    更新用户信息
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

//      删除用户
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteById(id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result list(){
        List<User> users = userService.list();
        return Result.success(users);

    }

    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest){
        return Result.success(userService.page(userPageRequest));

    }








}
